package com.lego.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lego.bean.Conment;
import com.lego.bean.Reviews;
import com.lego.bean.Seller;
import com.lego.bean.User;
import com.lego.bean.UserReviewVO;
import com.lego.dao.BaseDao;
import com.lego.dao.ReviewsDao;

public class ReviewsDaoImpl implements ReviewsDao {
	@Override
	public Reviews selectComment(int orderId, String rewContext) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		String sql = "SELECT * FROM reviews WHERE orderId = ?";
		con = BaseDao.getCon();
		Reviews reviews = null;
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, orderId);
			res = ps.executeQuery();
			if(res.next()) {
				reviews = new Reviews();
				reviews.setOrderId(res.getInt("orderId"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return reviews;
	}

	@Override
	public int upDateComment(String rewContext,int orderId) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "UPDATE reviews SET rewContext = ? WHERE orderId = ? ";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, rewContext);
			ps.setInt(2, orderId);
			System.out.println(ps);
			result = ps.executeUpdate();
		} catch (SQLException e) {			
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}

	@Override
	public int insertComment(int orderId, String rewContext,int userId,int goodsId) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "INSERT INTO reviews(orderId,goodsId,userId,reviewTime,rewContext) "
				+ "VALUES (?,?,?,now(),?)";
		con = BaseDao.getCon();
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, orderId);
			ps.setInt(2, goodsId);
			ps.setInt(3, userId);
			ps.setString(4, rewContext);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public int delComent(int reviewId) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "DELETE FROM reviews WHERE reviewId= ?";
		con = BaseDao.getCon();
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, reviewId);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	@Override
	public int delComentByUserId(int userId) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "DELETE FROM reviews WHERE userId= ?";
		con = BaseDao.getCon();
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, userId);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<Conment> selectCmt() {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		List<Conment> list = new ArrayList<Conment>();
		String sql = " SELECT reviewId,nickName,goodsName,reviewTime,rewContext,goodsType FROM orders AS o JOIN goods AS g JOIN reviews AS r JOIN `user` as u  "
				+ "WHERE o.orderId = r.orderId and o.goodsId = r.goodsId and o.userId = r.userId "
				+ " AND g.goodsId = o.goodsId "
				+ " and o.userId = u.userId "
				+ " and r.userId = u.userId";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			System.out.println(ps);
			res = ps.executeQuery();
			while(res.next()) {
				Conment conment=new Conment();
			    conment.setReviewId(res.getInt("reviewId"));
			    conment.setNickName(res.getString("nickName"));
			    conment.setReviewTime(res.getDate("reviewTime"));
			    conment.setRewContext(res.getString("rewContext"));
			    conment.setGoodsName(res.getString("GoodsName"));
			    conment.setGoodsType(res.getString("goodsType"));
			    list.add(conment);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return list;
	}
}
